//package com.example.a7822.myapplication.shopcart.view;
//
//import android.content.Context;
//import android.content.Intent;
//import android.graphics.drawable.Drawable;
//import android.media.AudioManager;
//import android.media.SoundPool;
//import android.os.Bundle;
//import android.support.v4.widget.SwipeRefreshLayout;
//import android.support.v7.widget.DefaultItemAnimator;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.view.LayoutInflater;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.WindowManager;
//import android.view.animation.Animation;
//import android.view.animation.TranslateAnimation;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.example.a7822.myapplication.R;
//import com.example.a7822.myapplication.shopcart.SPCartManager;
//import com.example.a7822.myapplication.shopcart.SPShopManager;
//import com.example.a7822.myapplication.shopcart.beans.GoodBean;
//import com.example.a7822.myapplication.shopcart.presenter.GoodsPresenter;
//import com.example.a7822.myapplication.shopcart.presenter.GoodsPresenterImpl;
//import com.example.a7822.myapplication.shopcart.presenter.ShopDetailPresenter;
//import com.example.a7822.myapplication.shopcart.presenter.ShopDetailPresenterImpl;
//
//import java.util.List;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;
//
///**
// * Created by wp on 2016/6/14.
// */
//public class GoodsViewImpl extends SPBaseActivity implements ZXingScannerView.ResultHandler,GoodsView, SwipeRefreshLayout.OnRefreshListener {
//    private String tag = "GoodsViewImpl";
//    private GoodsPresenter presenter;
//    private ShopDetailPresenter shopPresenter;
//    private Logger logger = Logger.getLogger(GoodsViewImpl.class);
//    private SwipeRefreshLayout mSwipeRefreshWidget;
//    private RecyclerView mRecyclerView;
//    private boolean mIsRefreshing = false;
//    private LinearLayoutManager mLayoutManager;
//    private CartAdapter mAdapter;
////    private List<GoodBean> mData = new ArrayList<GoodBean>();;
//    private String pageIndex = "-1";
//    private String curShopID = "0" ;
////    private IntentIntegrator integrator  = new IntentIntegrator(GoodsViewImpl.this);
//    @BindView(R.id.tv_shopcart_totalprice)
//TextView tvTotalPrice;
//    @BindView(R.id.tv_shopcart_totalnum)
//    TextView tvTotalNum;
//    @BindView(R.id.tv_shopcart_addselect)
//    TextView tvShopCartSelect;
//    @BindView(R.id.pay)
//    TextView btnPay;
//    @BindView(R.id.bottom_edit_container)
//    View layBottomEdit;
//    @BindView(R.id.bottom_container)
//    View layBottom;
//    @BindView(R.id.act_delete)
//    View btnDelete;
////    @BindView(R.id.rlShoppingCartEmpty)  RelativeLayout layCartEmpty;
//    private boolean isOrderFinished = false;
//    private double mTotalPrice1;
//    private boolean mSelect =false;
//    private Context context = this;
//    private boolean isEditing = false;
//    ZXingScannerView mScannerView;
//    String LastCode="";
//    SoundPool sp;
//    int sound;
//
//    EditText Etcode;
//    Button btn;
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//
//        presenter = new GoodsPresenterImpl(this);
//
//        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
//        sound =sp.load(this, R.raw.dd, 1);
//
//        initRes();
//        loadInfo();
//        updateInfo();
//        onRefresh();
//    }
//
//    public void quitShop(){
//        final UIAlertView delDialog = new UIAlertView(context, "退出商店", String.format("退出商店将清空购物车，确定吗？"),
//                "确认", "取消");
//        delDialog.show();
//
//        delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {
//            @Override
//            public void doLeft() {
//                 SPCartManager.getInstance().clearAll();
//                delDialog.dismiss();
//                finish();
//            }
//
//            @Override
//            public void doRight() {
//                delDialog.dismiss();
//            }
//        });
//    }
//
//    public void loadInfo(){
//
//        //top title
//        shopPresenter = new ShopDetailPresenterImpl(this);
//        String shopID = getIntent().getExtras().getString("shopID");
//        shopPresenter.loadShopDetail(shopID);
//        logger.d("shopID: "+shopID);
//        curShopID = shopID;
//
//        //存储当前ShopID
//        SPCartManager.getInstance().setCurShopID(curShopID);
//
//        //top right button
//        //设置扫描商品功能
//        setRightButton(R.drawable.icon_scan_white);
//        setLeftButton(R.drawable.icon_arrow_back);
//
//
//
//        setRightBtn2Icon(R.drawable.icon_editor);
//
//
//
//    }
//
//    public void updateInfo(){
//
//        int totalCount = SPCartManager.getInstance().getTotalCount();
//        double totalPrice = SPCartManager.getInstance().getTotalPrice();
//        if (totalPrice <= 0){
//            totalPrice = 0;
//            btnPay.setEnabled(false);
//            btnDelete.setEnabled(false);
//
//            tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(getDrawable(R.drawable.shopcart_unselected),null,null,null);
//            tvShopCartSelect.setEnabled(false);
//
////            layCartEmpty.setVisibility(View.VISIBLE);
//        }else {
//            btnPay.setEnabled(true);
////            btnDelete.setEnabled(true);
////            tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(getDrawable(R.drawable.shopcart_selected),null,null,null);
//            tvShopCartSelect.setEnabled(true);
//        }
//
//
//        tvTotalPrice.setText(String.format("总价：¥%.2f",totalPrice));
//        tvTotalNum.setText(String.format("共%d件商品",totalCount));
//        mTotalPrice1 = totalPrice;
//    }
//
//    public void initRes(){
//
//
//
//        // 绑定布局资源(注意放所有资源初始化之前)
//        LayoutInflater.from(this).inflate(R.layout.sp_activity_shopcart, topContentView);
//        setTitle(R.string.activity_title_shopcart);
//
//        Etcode = (EditText)findViewById(R.id.Etcode);
//         btn = (Button)findViewById(R.id.btn);
//        btn.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View view) {
//                String code = Etcode.getText().toString();
//                if(code==null||code.equals("")){
//                    Toast.makeText(GoodsViewImpl.this,"请先输入条形码", Toast.LENGTH_SHORT).show();
//                }else{
//                    synchronized(LastCode)
//                    {
//                        if(LastCode.equals(code)){
//                            return;
//                        }
//                        presenter.loadOneGood(code);
//                        mAdapter.refreshData();
//                        updateInfo();
//
//                        mRecyclerView.smoothScrollBy(1,500);
//                        LastCode =code;
//                        Etcode.setText("");
//                    }
//                }
//            }
//        });
//
//        mScannerView = (ZXingScannerView) findViewById(R.id.zXingScannerView);
//        mScannerView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mScannerView.setVisibility(View.GONE);
//                mScannerView.stopCamera();
//            }
//        });
//
//        mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
//        mSwipeRefreshWidget.setColorSchemeResources(R.color.news_primary,
//                R.color.news_primary_dark, R.color.news_primary_light,
//                R.color.news_accent);
//        mSwipeRefreshWidget.setOnRefreshListener(this);
//
//        mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
//        mRecyclerView.setHasFixedSize(true);
//
//        mLayoutManager = new LinearLayoutManager(this);
//        mRecyclerView.setLayoutManager(mLayoutManager);
//
//        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mAdapter = new CartAdapter(this);
////        mAdapter.setOnItemClickListener(mOnItemClickListener);
//        mRecyclerView.setAdapter(mAdapter);
//
//        mRecyclerView.setOnTouchListener(//静止下拉刷新时，向上滑动操作,recycle自带bug
//                new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        if (mIsRefreshing) {
//                            return true;
//                        } else {
//                            return false;
//                        }
//                    }
//                }
//        );
//
//        ButterKnife.bind(this);
//
//        //初始化支付按钮
//        btnPay.setEnabled(false);
//
//        //修改数量接口
//        mAdapter.setOnEditClickListener(new CartAdapter.OnEditClickListener() {
//            @Override
//            public void onEditClick(int position, int cartid, int count) {
//                updateInfo();
//            }
//        });
//        //实时监控全选按钮
//        mAdapter.setResfreshListener(new CartAdapter.OnResfreshListener() {
//            @Override
//            public void onResfresh( boolean isAllSelect, boolean isOneSelect) {
//                mSelect = isAllSelect;
//                if(isAllSelect){
//                    Drawable left = getDrawable(R.drawable.shopcart_selected);
//                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
//                }else {
//                    Drawable left = getDrawable(R.drawable.shopcart_unselected);
//                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
//                }
//                if (isOneSelect){
//                    btnDelete.setEnabled(true);
//                }else{
//                    btnDelete.setEnabled(false);
//                }
//            }
//        });
//
//        //全选
//        tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mSelect = !mSelect;
//                if(mSelect){
//                    Drawable left = getDrawable(R.drawable.shopcart_selected);
//                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
//                    for(GoodBean good : SPCartManager.getInstance().getGoodList()){
//                        good.setSelected(true);
//                    }
//                }else{
//                    Drawable left = getDrawable(R.drawable.shopcart_unselected);
//                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
//                    for(GoodBean good : SPCartManager.getInstance().getGoodList()){
//                        good.setSelected(false);
//                    }
//                }
//                mAdapter.notifyDataSetChanged();
//
//            }
//        });
//
//
//
//    }
//    /*view 行为*/
//    @Override
//    public void showProcessBar() {
//        LogUtils.v(tag, "showProcessBar");
//        mSwipeRefreshWidget.setRefreshing(true);
//    }
//
//
//
//    @Override
//    public void addGood(List<GoodBean> goodBeanList) {
//
//    }
//
//    @Override
//    public void addGood(GoodBean goodBean) {
//        //没有找到对应商品
//        if (goodBean == null ) {
////            Toast.makeText(this, R.string.toast_no_relevant_good, Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        GoodBean existGood = SPCartManager.getInstance().getGood(goodBean.getGoodId());
//        if (existGood != null){
//            existGood.setGoodAmount(existGood.getGoodAmount() + 1);
//        }else {
//            SPCartManager.getInstance().getGoodList().add(goodBean);
//        }
//
//        AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//        float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//        float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//        float volumnRatio = volumnCurrent / audioMaxVolumn;
//        sp.play(sound, volumnRatio, volumnRatio, 1, 0,  1f);
//
//        mAdapter.refreshData();
//        mIsRefreshing = false;
//        updateInfo();
//    }
//
//    @Override
//    public void dismissProcessBar() {
//        LogUtils.v(tag, "dismissProcessBar");
//        mSwipeRefreshWidget.setRefreshing(false);
//        mIsRefreshing = false;
//    }
//
//    @Override
//    public void showErrorMsg() {
//        LogUtils.v(tag, "showErrorMsg");
//
////        mAdapter.notifyDataSetChanged();
//
//    }
//
//    @Override
//    public void showShopInfo() {
//        String shopName = SPShopManager.curShop.getShopName();
//
//        setTitle(shopName+"-"+getString(R.string.activity_title_shopcart));
//    }
//    /*view 行为*/
//
//    @Override
//    public void onRefresh() {
//        mIsRefreshing = true;
//        pageIndex = "-1";
//        /*if (mData != null) {
//            mData.clear();
//        }*/
////        presenter.loadCart(0, pageIndex);
//
//        dismissProcessBar();
//
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        mScannerView.stopCamera();
//    }
//
//    public void removeAllView(){
//        if (mRecyclerView != null) {
//            mRecyclerView.removeAllViews();
//        }
//    }
//
//
//
//    public void onClick(View v){
//        switch (v.getId()){
//            //top right
//            case R.id.right_btn:
//                Animation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
//                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
//                        -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
//                mShowAction.setDuration(700);
//                mScannerView.startAnimation(mShowAction);
//                mScannerView.setVisibility(View.VISIBLE);
//                mScannerView.startCamera();
//                mScannerView.setAutoFocus(true);
//
//                logger.d("扫描商品条码");
//                break;
//            case R.id.left_btn:
//                quitShop();
//                break;
//            case R.id.pay:
//                startActivity(new Intent(this,SubmitOrderActivity.class));
//                break;
//            case R.id.right_btn2:
//                isEditing = !isEditing;
//                if (isEditing) {
//                    topRightBtn.setEnabled(false);
//                    setRightBtn2Icon(R.drawable.icon_right);
//                    layBottom.setVisibility(View.GONE);
//                    layBottomEdit.setVisibility(View.VISIBLE);
//
//                }
//                else {
//                    topRightBtn.setEnabled(true);
//                    setRightBtn2Icon(R.drawable.icon_editor);
//                    layBottom.setVisibility(View.VISIBLE);
//                    layBottomEdit.setVisibility(View.GONE);
//                }
//                mAdapter.setIsEditing(isEditing);
//                break;
//            case R.id.act_delete:
//                mAdapter.deleteItems();
//                updateInfo();
//                break;
//
//        }
//    }
////    @Override
////    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
////        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
////        if (result != null) {
////            if (result.getContents() == null) {
////                Toast.makeText(this, "扫码取消！", Toast.LENGTH_LONG).show();
////            } else {
////                logger.d("扫码结果为："+result.getContents());
////                //添加商品
////                String goodCode = result.getContents();
////                presenter.loadOneGood(goodCode);
////            }
////        } else {
////            super.onActivityResult(requestCode, resultCode, data);
////        }
////    }
//
//
//    @Override
//    public void handleResult(Result result) {
//        mScannerView.resumeCameraPreview(GoodsViewImpl.this);
//
//        String text =  result.getText();
//        synchronized(LastCode)
//        {
//            if(LastCode.equals(text)){
//                return;
//            }
//            presenter.loadOneGood(text);
//            mAdapter.refreshData();
//            updateInfo();
//
////            AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
////            float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
////            float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
////            float volumnRatio = volumnCurrent / audioMaxVolumn;
////            sp.play(sound, volumnRatio, volumnRatio, 1, 0,  1f);
//
//            mRecyclerView.smoothScrollBy(1,500);
//            LastCode =text;
//        }
//    }
//
//
//
//    @Override
//    protected void onDestroy() {
//
//        logger.d("onDestroy");
//
//
//        super.onDestroy();
//    }
//
//
//    @Override
//    protected void onResume() {
//
//        mScannerView.setResultHandler(GoodsViewImpl.this);
//        mScannerView.startCamera();
//        mScannerView.setAutoFocus(true);
//
//        mAdapter.refreshData();
//        updateInfo();
//        super.onResume();
//    }
//}
